#!/bin/bash

#
# This script will create a u-boot binary for sd/mmc boot
#

PWD=$(pwd)

CONFIG_1G_DDR=1

if [[ CONFIG_1G_DDR -eq 1 ]]
then
echo "use 1G ddr memory !!!!!!"
#####CONFIG_UT_1G_DDR
BL1_img=./blx_4412/E4412_N.bl1.bin
TZSW_img=./blx_4412/E4412_tzsw.bin
else
echo "use 2G ddr memory !!!!!!"
#####CONFIG_UT_2G_DDR
BL1_img=./blx_4412/E4412_N.bl1.SCP2G.bin
TZSW_img=./blx_4412/tzsw_SMDK4412_SCP_2GB.bin
fi


#BL2_img=./blx_4412/E4412_bl2.bin
BL2_img=./bl2.bin

uboot_Image=bootloader_sd.vhd
uboot_eMMC_Image=bootloader_mmc.bin

# creating empty env space (16kb)
dd if=/dev/zero of=empty.env bs=2048 count=1 2> /dev/null
dd if=/dev/zero of=empty_1M.env bs=1M count=1 2> /dev/null

function mksd_image()
{
    rm -f $1

    
    # merging the bl1 and env with padded 512k binary bl2.bin
    cat sd_mbr $BL1_img $BL2_img empty.env u-boot.bin empty_1M.env > $1
#    sync
    
    split -b 368128 $1 tmp_Image 
    mv tmp_Imageaa $1
    
    
    cat $TZSW_img >> $1
    cat empty_1M.env >> $1
    
    split -b 1M $1 uboot-sd.bin
    mv uboot-sd.binaa uboot-sd.bin
    cp uboot-sd.bin $1 -rf
#    sync

    rm uboot-sd.bin*
    rm tmp_Imagea*
    chmod 777 $1
}

function mkemmc_image()
{
    rm -f $1

    # make eMMC4.3 image $BL2_img bl2.bin
    cat $BL1_img $BL2_img empty.env u-boot.bin empty_1M.env > $1
    
#    sync
    
    split -b 367616 $1 tmp1_Image 
    mv tmp1_Imageaa $1
    
    
    cat $TZSW_img >> $1
    cat empty_1M.env >> $1
    
    split -b 1M $1 uboot-mmc.bin
    mv uboot-mmc.binaa uboot-mmc.bin
    cp uboot-mmc.bin $1 -rf

#    sync

    rm uboot-mmc.bin*
    rm tmp1_Imagea*
    chmod 777 $1
}

mksd_image $uboot_Image
mkemmc_image $uboot_eMMC_Image

# removing temp files
rm -f empty.env
rm empty_1M.env


#sync

echo "              [[[  gen 4412 ]]]                                   "
echo "your can write the uboot image at uboot cmd console ,"
echo "load $uboot_Image to 0x40008000, and write image to mmc ,"
echo "for example: mmc write 0 40008000 0 0x800"
echo "                                                 "
